<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Logging user defined types</title>
<link rel="stylesheet" href="../../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../index.html" title="Boost.Test">
<link rel="up" href="../test_tools_support_for_logging.html" title="Tools supports for logging">
<link rel="prev" href="../test_tools_support_for_logging.html" title="Tools supports for logging">
<link rel="next" href="test_output_macro_message.html" title="Custom messages">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../test_tools_support_for_logging.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../test_tools_support_for_logging.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="test_output_macro_message.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="boost_test.test_output.test_tools_support_for_logging.testing_tool_output_disable"></a><a class="link" href="testing_tool_output_disable.html" title="Logging user defined types">Logging
        user defined types</a>
</h4></div></div></div>
<p>
          Most of the <a class="link" href="../../testing_tools.html" title="Writing unit tests">testing tools</a>
          print values of their arguments to the output stream in some form of log
          statement. If arguments type does not support
        </p>
<pre class="programlisting"><span class="keyword">operator</span><span class="special">&lt;&lt;(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span><span class="special">&amp;,</span> <span class="identifier">ArgumentType</span> <span class="keyword">const</span><span class="special">&amp;);</span>
</pre>
<p>
          interface, you will get a compilation error.
        </p>
<p>
          The <span class="emphasis"><em>Unit Test Framework</em></span> supports three different methods
          for logging user defined types:
        </p>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
              through the <code class="computeroutput"><span class="keyword">operator</span><span class="special">&lt;&lt;</span></code>
              for that specific type: any type that implements the above interface
              has direct support for logging,
            </li>
<li class="listitem">
              through a customization point responsible for logging a specific type,
              which is less intrusive than the implementation of <code class="computeroutput"><span class="keyword">operator</span><span class="special">&lt;&lt;</span></code>. This is explained in more
              details in <a class="link" href="testing_tool_output_disable.html#ref_log_output_custom_customization_point">this
              section</a>,
            </li>
<li class="listitem">
              by prohibiting the <a class="link" href="../../testing_tools.html" title="Writing unit tests">testing
              tools</a> from logging argument values for specified type through
              <a class="link" href="../../utf_reference/testout_reference/test_output_macro_disable_type.html" title="BOOST_TEST_DONT_PRINT_LOG_VALUE"><code class="computeroutput"><span class="identifier">BOOST_TEST_DONT_PRINT_LOG_VALUE</span></code></a>.
              This is explained in more details in <a class="link" href="testing_tool_output_disable.html#ref_log_output_custom_avoid_printing">this
              section</a>.
            </li>
</ol></div>
<a name="ref_log_output_custom_customization_point"></a><h5>
<a name="boost_test.test_output.test_tools_support_for_logging.testing_tool_output_disable.h0"></a>
          <span class="phrase"><a name="boost_test.test_output.test_tools_support_for_logging.testing_tool_output_disable.user_type_customization_point_fo"></a></span><a class="link" href="testing_tool_output_disable.html#boost_test.test_output.test_tools_support_for_logging.testing_tool_output_disable.user_type_customization_point_fo">User
          type customization point for logging</a>
        </h5>
<p>
          It is possible to indicate a function, <code class="computeroutput"><span class="identifier">boost_test_print_type</span></code>,
          to <span class="emphasis"><em>Unit Test Framework</em></span> that is responsible for the
          printing of a user defined type, without the need to override the <code class="computeroutput"><span class="keyword">operator</span><span class="special">&lt;&lt;</span></code>
          for that specific type. This is convenient for instance when the <code class="computeroutput"><span class="keyword">operator</span><span class="special">&lt;&lt;</span></code>
          has already been defined for other needs.
        </p>
<p>
          The syntax follows the <code class="computeroutput"><span class="keyword">operator</span><span class="special">&lt;&lt;</span></code>, and this function should be in
          the same namespace as the type:
        </p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span><span class="special">&amp;</span> <span class="identifier">boost_test_print_type</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span><span class="special">&amp;</span> <span class="identifier">ostr</span><span class="special">,</span> <span class="identifier">ArgumentType</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">right</span><span class="special">);</span>
</pre>
<h6>
<a name="boost_test.test_output.test_tools_support_for_logging.testing_tool_output_disable.h1"></a>
          <span class="phrase"><a name="boost_test.test_output.test_tools_support_for_logging.testing_tool_output_disable.example_descr"></a></span><a class="link" href="testing_tool_output_disable.html#boost_test.test_output.test_tools_support_for_logging.testing_tool_output_disable.example_descr">Example:
          Logging customization point usage</a>
        </h6>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Code
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_TEST_MODULE</span> <span class="identifier">logger</span><span class="special">-</span><span class="identifier">customization</span><span class="special">-</span><span class="identifier">point</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">included</span><span class="special">/</span><span class="identifier">unit_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">namespace</span> <span class="identifier">user_defined_namespace</span> <span class="special">{</span>
  <span class="keyword">struct</span> <span class="identifier">user_defined_type</span> <span class="special">{</span>
      <span class="keyword">int</span> <span class="identifier">value</span><span class="special">;</span>

      <span class="identifier">user_defined_type</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">value_</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">value</span><span class="special">(</span><span class="identifier">value_</span><span class="special">)</span>
      <span class="special">{}</span>

      <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">==(</span><span class="keyword">int</span> <span class="identifier">right</span><span class="special">)</span> <span class="keyword">const</span> <span class="special">{</span>
          <span class="keyword">return</span> <span class="identifier">right</span> <span class="special">==</span> <span class="identifier">value</span><span class="special">;</span>
      <span class="special">}</span>
  <span class="special">};</span>
<span class="special">}</span>

<span class="keyword">namespace</span> <span class="identifier">user_defined_namespace</span> <span class="special">{</span>
  <span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span><span class="special">&amp;</span> <span class="identifier">boost_test_print_type</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span><span class="special">&amp;</span> <span class="identifier">ostr</span><span class="special">,</span> <span class="identifier">user_defined_type</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">right</span><span class="special">)</span> <span class="special">{</span>
      <span class="identifier">ostr</span> <span class="special">&lt;&lt;</span> <span class="string">"** value of user_defined_type is "</span> <span class="special">&lt;&lt;</span> <span class="identifier">right</span><span class="special">.</span><span class="identifier">value</span> <span class="special">&lt;&lt;</span> <span class="string">" **"</span><span class="special">;</span>
      <span class="keyword">return</span> <span class="identifier">ostr</span><span class="special">;</span>
  <span class="special">}</span>
<span class="special">}</span>

<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">test1</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">user_defined_namespace</span><span class="special">::</span><span class="identifier">user_defined_type</span> <span class="identifier">t</span><span class="special">(</span><span class="number">10</span><span class="special">);</span>
    <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">t</span> <span class="special">==</span> <span class="number">11</span><span class="special">);</span>

    <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">user_defined_namespace</span><span class="special">;</span>
    <span class="identifier">user_defined_type</span> <span class="identifier">t2</span><span class="special">(</span><span class="number">11</span><span class="special">);</span>
    <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">t2</span> <span class="special">==</span> <span class="number">11</span><span class="special">);</span>
<span class="special">}</span>
</pre>
                </td></tr></tbody>
</table></div>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Output
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="special">&gt;</span> <span class="identifier">logger</span><span class="special">-</span><span class="identifier">customization</span><span class="special">-</span><span class="identifier">point</span> <span class="special">--</span><span class="identifier">log_level</span><span class="special">=</span><span class="identifier">all</span>
<span class="identifier">Running</span> <span class="number">1</span> <span class="identifier">test</span> <span class="keyword">case</span><span class="special">...</span>
<span class="identifier">Entering</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"logger-customization-point"</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">36</span><span class="special">):</span> <span class="identifier">Entering</span> <span class="identifier">test</span> <span class="keyword">case</span> <span class="string">"test1"</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">39</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test1"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">t</span> <span class="special">==</span> <span class="number">11</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[**</span> <span class="identifier">value</span> <span class="identifier">of</span> <span class="identifier">user_defined_type</span> <span class="identifier">is</span> <span class="number">10</span> <span class="special">**</span> <span class="special">!=</span> <span class="number">11</span><span class="special">]</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">43</span><span class="special">):</span> <span class="identifier">info</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">t2</span> <span class="special">==</span> <span class="number">11</span> <span class="identifier">has</span> <span class="identifier">passed</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">36</span><span class="special">):</span> <span class="identifier">Leaving</span> <span class="identifier">test</span> <span class="keyword">case</span> <span class="string">"test1"</span><span class="special">;</span> <span class="identifier">testing</span> <span class="identifier">time</span><span class="special">:</span> <span class="number">125u</span><span class="identifier">s</span>
<span class="identifier">Leaving</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"logger-example-customization-point"</span><span class="special">;</span> <span class="identifier">testing</span> <span class="identifier">time</span><span class="special">:</span> <span class="number">157u</span><span class="identifier">s</span>

<span class="special">***</span> <span class="number">1</span> <span class="identifier">failure</span> <span class="identifier">is</span> <span class="identifier">detected</span> <span class="identifier">in</span> <span class="identifier">the</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"logger-customization-point"</span>
</pre>
                </td></tr></tbody>
</table></div>
<a name="ref_log_output_custom_avoid_printing"></a><h5>
<a name="boost_test.test_output.test_tools_support_for_logging.testing_tool_output_disable.h2"></a>
          <span class="phrase"><a name="boost_test.test_output.test_tools_support_for_logging.testing_tool_output_disable.prohibiting_the_printing_of_a_sp"></a></span><a class="link" href="testing_tool_output_disable.html#boost_test.test_output.test_tools_support_for_logging.testing_tool_output_disable.prohibiting_the_printing_of_a_sp">Prohibiting
          the printing of a specific type</a>
        </h5>
<p>
          To prohibit the printing of a specific type, use the following statement
          on file level before first test case that includes statement failing to
          compile:
        </p>
<pre class="programlisting"><span class="identifier">BOOST_TEST_DONT_PRINT_LOG_VALUE</span><span class="special">(</span><span class="identifier">ArgumentType</span><span class="special">)</span>
</pre>
<h6>
<a name="boost_test.test_output.test_tools_support_for_logging.testing_tool_output_disable.h3"></a>
          <span class="phrase"><a name="boost_test.test_output.test_tools_support_for_logging.testing_tool_output_disable.example_descr0"></a></span><a class="link" href="testing_tool_output_disable.html#boost_test.test_output.test_tools_support_for_logging.testing_tool_output_disable.example_descr0">Example:
          BOOST_TEST_DONT_PRINT_LOG_VALUE usage</a>
        </h6>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Code
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_TEST_MODULE</span> <span class="identifier">example</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">included</span><span class="special">/</span><span class="identifier">unit_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">utility</span><span class="special">&gt;</span>


<span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span><span class="keyword">float</span><span class="special">&gt;</span> <span class="identifier">pair_type</span><span class="special">;</span>

<span class="identifier">BOOST_TEST_DONT_PRINT_LOG_VALUE</span><span class="special">(</span> <span class="identifier">pair_type</span> <span class="special">)</span>

<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span> <span class="identifier">test_list</span> <span class="special">)</span>
<span class="special">{</span>
  <span class="identifier">pair_type</span> <span class="identifier">p1</span><span class="special">(</span> <span class="number">2</span><span class="special">,</span> <span class="number">5.5f</span> <span class="special">);</span>
  <span class="identifier">pair_type</span> <span class="identifier">p2</span><span class="special">(</span> <span class="number">2</span><span class="special">,</span> <span class="number">5.501f</span> <span class="special">);</span>

  <span class="identifier">BOOST_CHECK_EQUAL</span><span class="special">(</span> <span class="identifier">p1</span><span class="special">,</span> <span class="identifier">p2</span> <span class="special">);</span>
<span class="special">}</span>
</pre>
                </td></tr></tbody>
</table></div>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Output
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="identifier">Running</span> <span class="number">1</span> <span class="identifier">test</span> <span class="keyword">case</span><span class="special">...</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">16</span><span class="special">):</span> <span class="identifier">error</span> <span class="identifier">in</span> <span class="string">"test_list"</span><span class="special">:</span> <span class="identifier">check</span> <span class="identifier">p1</span> <span class="special">==</span> <span class="identifier">p2</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span> <span class="special">!=</span> <span class="special">]</span>

<span class="special">***</span> <span class="number">1</span> <span class="identifier">failures</span> <span class="identifier">is</span> <span class="identifier">detected</span> <span class="identifier">in</span> <span class="identifier">test</span> <span class="identifier">suite</span> <span class="string">"example"</span>
</pre>
                </td></tr></tbody>
</table></div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2001-2020 Boost.Test contributors<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../test_tools_support_for_logging.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../test_tools_support_for_logging.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="test_output_macro_message.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
